package com.dlyk.web;

import com.dlyk.result.NameValue;
import com.dlyk.result.R;
import com.dlyk.result.SummaryData;
import com.dlyk.service.StatisticService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: ajie
 * @date: 2024/8/7 22:05:28
 * @version: 1.0
 */
@RestController
public class StatisticController {

    @Resource
    private StatisticService statisticService;

    /**
     * @description: 查询概览统计数据 
     * @params: []       
     * @return: 
     * @author: ajie
     * @date: 2024/8/10 15:32
     */
    @GetMapping("api/summary/data")
    public R summaryData(){
        SummaryData summaryData = statisticService.loadSummaryData();
        return R.OK(summaryData);
    }

    /**
     * @description: 查询销售漏斗数据
     * @params: []       
     * @return: 
     * @author: ajie
     * @date: 2024/8/10 15:32
     */
    @GetMapping("api/saleFunnel/data")
    public R saleFunnelData(){
        /*data: [
                  {value: 20, name: '成交'},
                  {value: 60, name: '交易'},
                  {value: 80, name: '客户'},
                  {value: 100, name: '线索'},
                ]
         */
        List<NameValue> nameValueList = statisticService.loadSaleFunnelData();
        return R.OK(nameValueList);
    }

    /**
     * @description: 查询线索来源数据
     * @params: []       
     * @return: 
     * @author: ajie
     * @date: 2024/8/10 18:10
     */
    @GetMapping("api/sourcePie/data")
    public R sourcePieData(){
        /*data: [
                  {value: 1048, name: 'Search Engine'},
                  {value: 735, name: 'Direct'},
                  {value: 580, name: 'Email'},
                  {value: 484, name: 'Union Ads'},
                  {value: 300, name: 'Video Ads'}
                ],
          */
        List<NameValue> nameValueList = statisticService.loadSourcePieData();
        return R.OK(nameValueList);
    }

    /**
     * 获取市场活动柱状图的数据
     *
     * @return
     */
    @GetMapping(value = "/api/statistic/activityBarChart")
    public R activityBarChart() {
        /**
         * 每个月的数据按如下的数组格式返回即可
         * [120, 200, 150, 80, 70, 110, 130],
         */
        Integer[] activityDataArray = statisticService.getActivityBarChartData();
        return R.OK(activityDataArray);
    }


    /**
     * 获取线索柱状图的数据
     *
     * @return
     */
    @GetMapping(value = "/api/statistic/clueBarChart")
    public R clueBarChart() {
        //x轴的数据数组
        int days = LocalDate.now().lengthOfMonth();
        Integer[] xDataArray = new Integer[days];
        int a = 0;
        for (int i = 1; i <= days; i++) {
            xDataArray[a] = i;
            a ++;
        }

        /**
         * 每天的数据按如下的数组格式返回即可
         * [120, 200, 150, 80, 70, 110, 130],
         */
        Integer[] yDataArray = statisticService.getClueBarChartData();

        Map<String, Integer[]> resultMap = new HashMap<>();
        resultMap.put("x", xDataArray);
        resultMap.put("y", yDataArray);
        return R.OK(resultMap);
    }

    /**
     * 获取客户柱状图的数据
     *
     * @return
     */
    @GetMapping(value = "/api/statistic/customerBarChart")
    public R customerBarChart() {
        //x轴的数据数组
        int days = LocalDate.now().lengthOfMonth();
        Integer[] xDataArray = new Integer[days];
        int a = 0;
        for (int i = 1; i <= days; i++) {
            xDataArray[a] = i;
            a ++;
        }

        /**
         * 每天的数据按如下的数组格式返回即可
         * [120, 200, 150, 80, 70, 110, 130],
         */
        Integer[] yDataArray = statisticService.getCustomerBarChartData();

        Map<String, Integer[]> resultMap = new HashMap<>();
        resultMap.put("x", xDataArray);
        resultMap.put("y", yDataArray);
        return R.OK(resultMap);
    }

    /**
     * 获取交易柱状图的数据
     *
     * @return
     */
    @GetMapping(value = "/api/statistic/tranBarChart")
    public R tranBarChart() {
        //x轴的数据数组
        int days = LocalDate.now().lengthOfMonth();
        Integer[] xDataArray = new Integer[days];
        int a = 0;
        for (int i = 1; i <= days; i++) {
            xDataArray[a] = i;
            a ++;
        }

        /**
         * 每天总的交易数据按如下的数组格式返回即可
         * [120, 200, 150, 80, 70, 110, 130],
         */
        BigDecimal[] yDataArray1 = statisticService.getTranBarChartData();

        /**
         * 每天成功的交易数据按如下的数组格式返回即可
         * [120, 200, 150, 80, 70, 110, 130],
         */
        BigDecimal[] yDataArray2 = statisticService.getSuccessTranBarChartData();

        Map<String, Object[]> resultMap = new HashMap<>();
        resultMap.put("x", xDataArray);
        resultMap.put("y1", yDataArray1);
        resultMap.put("y2", yDataArray2);
        return R.OK(resultMap);
    }
}
